package com.lanhe.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhe.order.domain.LhOrdersPool;
import com.lanhe.order.mapper.LhOrdersPoolMapper;
import com.lanhe.order.service.ILhOrdersPoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (LhOrdersPool)表服务实现类
 *
 * @author zhanggl
 * @since 2023-12-11 09:58:45
 */
@Service
public class LhOrdersPoolServiceImpl extends ServiceImpl<LhOrdersPoolMapper, LhOrdersPool> implements ILhOrdersPoolService {

	@Autowired
	private LhOrdersPoolMapper lhOrdersPoolMapper;

	/**
	 * 新增主订单数据
	 *
	 * @param ordersPool 实体
	 * @return int 返回值
	 * @author zhanggl
	 * @date 2023/12/11
	 */
	@Override
	public int insertOrdersPool(LhOrdersPool ordersPool) {

		LambdaQueryWrapper<LhOrdersPool> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhOrdersPool::getOrderId, ordersPool.getOrderId());
		queryWrapper.eq(LhOrdersPool::getOrigin, ordersPool.getOrigin());
		Long count = lhOrdersPoolMapper.selectCount(queryWrapper);
		if (count > 0) {

			ordersPool.setUpdateTime(new Date());
			return lhOrdersPoolMapper.update(ordersPool,queryWrapper);
		} else {
			ordersPool.setCreateTime(new Date());
			return lhOrdersPoolMapper.insert(ordersPool);
		}
	}

	/**
	 * 分页查询汇总订单
	 *
	 * @param orders 实体
	 * @return java.util.List<com.lanhe.order.domain.LhOrdersKayu>
	 * @author zhanggl
	 * @date 2023/12/27
	 */
	@Override
	public List<LhOrdersPool> selectOrdersList(LhOrdersPool orders) {
		LambdaQueryWrapper<LhOrdersPool> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhOrdersPool::getMemberId, orders.getMemberId());
		return lhOrdersPoolMapper.selectList(queryWrapper);
	}

	/**
	 * 根据origin和originId查询数据
	 *
	 * @param origin  源
	 * @param orderId 订单号
	 * @return int 返回值
	 * @author zhanggl
	 * @date 2024年01月04日15:14:37
	 */
	@Override
	public LhOrdersPool selectByOriginAndOrderId(Integer origin, Long orderId) {
		LambdaQueryWrapper<LhOrdersPool> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LhOrdersPool::getOrigin, origin);
		queryWrapper.eq(LhOrdersPool::getOrderId, orderId);
		return lhOrdersPoolMapper.selectOne(queryWrapper);
	}
}

